
/// funkcje pomocnicze

int mov(double*dst, double*src, int n){
    int i;
    #pragma omp parallel for shared(dst, src, n) private(i)
    for (i=0; i<n; ++i) dst[i] = src[i];
    return 0;
}

int movNeg(double*dst, double*src, int n){
    int i;
    #pragma omp parallel for shared(dst, src, n) private(i)
    for (i=0; i<n; ++i) dst[i] = -src[i];
    return 0;
}

void printV(char*descr, double*x, int n){
    int i;
    printf("%s", descr);
    printf("[");
    for (i=0; i<n; ++i) printf("%12g,",x[i]);
    printf("]\n");
}

void initMatrix(double**matrix, int n, double value){
    int i,j;
    #pragma omp parallel for shared(matrix, value, n) private(i,j)
    for (j=0;j<n;++j){
        for (i=0;i<n;++i){
            matrix[j][i] = value;
        }
    }
}

void initMatrixIdentity(double**matrix, int n, double value){
    int i,j;
    for (j=0;j<n;++j){
        for (i=0;i<n;++i){
            matrix[j][i] = 0;
            if (i==j){
                matrix[j][i] = value;
            }
        }
    }
}

void mulMatrixByVector(double**matrix, double*vector, int n, double*result){
    int i,j;
    double row;
    for (j=0;j<n;++j){
        row = 0.0;
        for (i=0;i<n;++i){
            row += matrix[j][i] * vector[j];
        }
        result[j] = row;
    }
}

void mulMatrixByMatrix(double**m1, double**m2, int n, double**result){
    int i,j,k;
    double cell;
    for (j=0;j<n;++j){
        for (i=0;i<n;++i){
            cell = 0.0;
            for (k=0;k<n;++k){
                cell += m1[j][k] * m2[k][i];
            }
            result[j][i] = cell;
        }
    }
}

void subVectors(double*v1, double*v2, int n, double*result){
    int i;
    #pragma omp parallel for shared(result, v1, v2, n) private(i)
    for (i=0;i<n;++i){
        result[i] = v1[i] - v2[i];
    }
}

void addVectors(double*v1, double*v2, int n, double*result){
    int i;
	#pragma omp parallel for shared(result, v1, v2, n) private(i)
    for (i=0;i<n;++i){
        result[i] = v1[i] + v2[i];
    }
}

float mulVectorTransByVector(double*v1, double*v2, int n){
    double result = 0;
    int i;
    for (i=0;i<n;++i){
        result += v1[i] * v2[i];
    }
    return result;
}

void mulVectorByVectorTrans(double*v1, double*v2, int n, double**result){
    int i,j;
    for (j=0;j<n;++j){
        for (i=0;i<n;++i){
            result[j][i] = v1[j] * v2[i];
        }
    }
}

void mulVectorByConstant(double*v1, double c, int n, double*result){
    int j;
    #pragma omp parallel for shared(result, v1, c, n) private(j)
    for (j=0;j<n;++j){
        result[j] = v1[j] * c;
    }
}

void mulMatrixByConstant(double**matrix, int n, double value, double**result){
    int i,j;
	#pragma omp parallel for shared(result, matrix, value, n) private(i,j)
    for (j=0;j<n;++j){
        for (i=0;i<n;++i){
            result[j][i] = value * matrix[j][i];
        }
    }
}

void subMatrix(double**m1, double**m2, int n, double**result){
    int i,j;
    #pragma omp parallel for shared(result, m1, m2, n) private(i,j)
    for (j=0;j<n;++j){
        for (i=0;i<n;++i){
            result[j][i] = m1[j][i] - m2[j][i];
        }
    }
}

void addMatrix(double**m1, double**m2, int n, double**result){
    int i,j;
    #pragma omp parallel for shared(result, m1, m2, n) private(i,j)
    for (j=0;j<n;++j){
        for (i=0;i<n;++i){
            result[j][i] = m1[j][i] + m2[j][i];
        }
    }
}

void transposeMatrix(double**m1, int n, double**result){
    int i,j;
    double b;
    #pragma omp parallel for shared(result, m1, n) private(i,j,b)
    for (j=0;j<n;++j){
        for (i=0;i<n;++i){
            if (i!=j){
                b = m1[j][i];
                result[j][i] = m1[i][j];
                result[i][j] = b;
            }
        }
    }
}

void printMatrix(double**m1, int n){
    int i,j;
    for (j=0;j<n;++j){
        printf("[");
        for (i=0;i<n;++i){
            printf("%12g ", m1[j][i]);
        }
        printf("]\n");
    }
}
